<template>
  <div class="toplist-item">
    <!-- 图片 -->
    <img class="toplist-item-img" :src=" toplistItem.coverImgUrl+'?param=180y180'||''" alt="">
    <div class="toplist-item-music">
      <div v-for="(item,index) of showsong" :key="index" class="music-item" @dblclick="playmusic(item)">
        <div :class="{bluetext:index==0||index==1||index==2}" class="music-item-index">{{index+1}}</div>
        <!-- 处理作者名字位置 -->
        <div class="name-box">
          <div class="name">{{item.name}}</div>
          <div class="authors">
            <div v-for="(author,index) of musicauthorbyfn(item)" :key="index+'author'"
              @click.stop="$router.push({ name: 'artistAccount', query: { id:author.id } });" class="author">
              {{author.name}}</div>
          </div>
        </div>
      </div>
      <router-link tag="a" class="getAll" :to="{
        name:'playlist',
        query:{
          id:toplistItem.id
        }
      }">查看全部<span class="el-icon-arrow-right"></span></router-link>

    </div>
  </div>
</template>
<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';
import { getplaylisttrackAPI } from "@/api/playlist";
export default {
  name: "toplist-item",
  //import引入的组件需要注入到对象中才能使用
  components: {},
  props: {
    toplistItem: {
      require: true,
    },
  },
  data() {
    //这里存放数据
    return {
      showsong: [],
    };
  },
  //计算属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    async getSongs() {
      const data = await getplaylisttrackAPI(this.toplistItem.id, 5, 0);
      this.showsong = data.data.songs;
    },
    // 获取音乐作者
    musicauthorbyfn(currentMusic) {
      if (currentMusic) {
        if (currentMusic.ar) {
          let name = [];
          currentMusic.ar.forEach((element) => {
            name.push(element);
          });
          return name;
        }
        return "作者";
      } else {
        return "作者";
      }
    },
    // 双击播放音乐
    playmusic(val) {
      // 把播放状态转为正在播放
      this.$store.state.musicisPlay = true;
      // 提交数据到vuex，通过vuex把数据传递给播放组件
      this.$store.commit("addMusictoMusiclist", this.id);
      this.$store.commit("getcurrentIndex", val);
      this.$store.dispatch("getMusicDetail", val);
    },
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {
    this.getSongs();
  },
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {},
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
};
</script>
<style scoped lang='less'>
.toplist-item {
  display: flex;
  height: 240px;
  width: 1200px;
  flex-shrink: 0;
  .toplist-item-img {
    margin-top: 20px;
    height: 180px;
    width: 180px;
    border-radius: 5px;
  }
  .toplist-item-music {
    margin-top: 20px;
    width: 950px;
    margin-left: 30px;
    :nth-child(odd) {
      background-color: #fafafa;
    }
    :nth-child(even) {
      background-color: #ffffff;
    }
    .music-item:hover {
      background-color: #f0f0f0;
    }
    .music-item {
      width: 950px;
      height: 35px;
      border-radius: 5px;
      display: flex;
      .music-item-index {
        background-color: transparent;
        height: 35px;
        line-height: 35px;
        margin-left: 15px;
        margin-right: 15px;
        color: rgb(187, 187, 187);
      }
      .bluetext {
        color: #4faffa;
      }
      .name-box {
        flex-shrink: 0;
        width: 885px;
        display: flex;
        justify-content: space-between;
        background-color: transparent;
        .name {
          background-color: transparent;
          height: 35px;
          line-height: 35px;
          font-size: 15px;
          color: #292929;
          flex-shrink: 0;
        }
        .authors {
          white-space: nowrap;
          overflow: hidden;
          text-overflow: ellipsis;
          flex-shrink: 0;
          left: 0;
          height: 35px;
          line-height: 35px;
          font-size: 15px;
          color: #7c7c7c;
          background-color: transparent;
          .author {
            background-color: transparent;
            display: inline-block;
            cursor: pointer;
            margin-left: 10px;
          }
        }
      }
    }
    .getAll {
      cursor: pointer;
      display: inline-block;
      font-size: 15px;
      color: #555555;
      margin-top: 10px;
      text-decoration: none;
    }
  }
}
</style>